import {
  famousCategoryList
} from "../../utils/commondata.js";

import {
  request
} from "../../utils/request.js";
import regeneratorRuntime from '../../utils/runtime';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    famousCategoryList: famousCategoryList(),
    themeIndex: 0,
    typeList: famousCategoryList()[0].typeList,
    typeIndex: 0,
    famousList: [],
    type: '',
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadFamous(1, this.data.typeList[0].value);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {
      page,
      type
    } = this.data;
    page = page + 1;
    this.loadFamous(page, type);
  },
  /**
   * 主题选择
   * @param {*} e 
   */
  handleThemeSelect(e) {
    let {
      index
    } = e.currentTarget.dataset;
    this.setData({
      typeIndex: 0,
      themeIndex: index,
      typeList: famousCategoryList()[index].typeList,
      famousList: [],
    });
    this.loadFamous(1, famousCategoryList()[index].typeList[0].value);
  },
  /**
   * 类型选择
   * @param {*} e 
   */
  handleTypeSelect(e) {
    const {
      index
    } = e.currentTarget.dataset;
    this.setData({
      typeIndex: index,
      famousList: [],
    });
    this.loadFamous(1, this.data.typeList[index].value);
  },
  /**
   * 显示明细
   * @param {*} e 
   */
  handleShowChapter(e) {
    const {
      parentid
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/chapter/detail/index?leftFlag=true&chapterId=' + parentid,
    });
  },

  /**
   * 加载名句
   * @param {*} page 
   * @param {*} type 
   */
  async loadFamous(page, type) {
    const res = await request({
      url: "/famous/pageQuery?limit=10&typeList=" + type + "&page=" + page,
      method: "get"
    });
    if (res.code != 0) {
      return;
    }
    this.setData({
      page: page,
      type: type,
      famousList: [...this.data.famousList, ...res.data.list],
    });

  },

  /**
   * 取消收藏
   */
  async handleCancelCollect(e) {

    // 判断是否登录，如果没有登录，给出登录提示
    if (!this.validLogin()) {
      return;
    }

    let {
      id,
      index
    } = e.currentTarget.dataset;
    let openId = wx.getStorageSync("openId");
    const res = await request({
      url: "/collect/cancelCollect?contentId=" + id +
        "&contentType=famous" +
        "&openId=" + openId,
      method: "get"
    });
    if (res.code != 0) {
      return;
    }

    let famousList = this.data.famousList;
    famousList[index].collectFlag = 'N';
    this.setData({
      famousList: famousList
    });
  },
  /**
   * 收藏
   */
  async handleCollectContent(e) {

    // 判断是否登录，如果没有登录，给出登录提示
    if (!this.validLogin()) {
      return;
    }

    let {
      id,
      index
    } = e.currentTarget.dataset;
    let openId = wx.getStorageSync("openId");
    const res = await request({
      url: "/collect/collectContent?contentId=" + id +
        "&contentType=famous" +
        "&openId=" + openId,
      method: "get"
    });
    if (res.code != 0) {
      return;
    }
    let famousList = this.data.famousList;
    famousList[index].collectFlag = 'Y';
    this.setData({
      famousList: famousList
    });

  },
  /**
   * 验证是否登录
   */
  validLogin() {
    let openId = wx.getStorageSync("openId");
    if (!openId) {
      wx.showToast({
        title: "请登录后进行操作",
        duration: 1700
      });
      return false;
    }
    return true;
  },
})